package com.njust.diary.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.njust.diary.dao.UserDao;
import com.njust.diary.po.User;
import com.njust.diary.vo.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

public class UserService {
    private UserDao userDao =new UserDao();

public Result<User> userLogin(String userName,String userPwd){
Result<User> result =new Result<>();
//输入的用户名和密码回显在jsp的value中
User u=new User();
u.setUname(userName);
u.setUpwd(userPwd);
result.setResult(u);

if(StrUtil.isBlank(userName)|| StrUtil.isBlank(userPwd)){
    result.setCode(0);
   result.setMsg("用户名或密码不能为空！");
   return result;
}
User user=userDao.queryUser(userName);
if(user==null){
    result.setCode(0);
    result.setMsg("用户不存在！");
    return result;
}
//userPwd = DigestUtil.md5Hex(userPwd);//密码加密
    if(!userPwd.equals(user.getUpwd())){
        result.setCode(0);
        result.setMsg("密码错误!");
        return result;
    }
result.setCode(1);
    result.setResult(user);




return result;
}


    public Integer checkNick(String nick, Integer userId) {
    if(StrUtil.isBlank((nick))){
        return 0;
    }
    User user =userDao.queryUserByNickAndUserId(nick,userId);
    if(user!=null){
        return 0;
    }
    return 1;


    }

    public Result<User> updateUser(HttpServletRequest req) {
      Result<User> result =new Result<>();
      String nick =req.getParameter("nick");
        String motto =req.getParameter("motto");
      if(StrUtil.isBlank(nick)){
          result.setCode(0);
          result.setMsg("用户昵称不能为空!");
          return result;
      }

             User user= (User) req.getSession().getAttribute("user");
            user.setNick(nick);
            user.setMotto(motto);
            try{
                //获取上传的part对象
                Part part =req.getPart("img");
                //获取part对象中文件名
                String header =part.getHeader("Content-Disposition");
                String str =header.substring(header.lastIndexOf("=")+2);
                //获取上传文件名
                String fileName=str.substring(0,str.length()-1);
                if(!StrUtil.isBlank(fileName)){
                    user.setHead(fileName);
                    String filePath =req.getServletContext().getRealPath("/sources/images/");
                    part.write(filePath+"/"+fileName);
                }


            }catch (Exception e){
                e.printStackTrace();
            }


            int row =userDao.updateUser(user);
            if(row>0){
                result.setCode(1);
                req.getSession().setAttribute("user",user);
            }else{
                result.setCode(0);
                result.setMsg("更新信息失败");
            }
        return result;


    }

    public Result<User> regist(HttpServletRequest req){
        Result<User> result =new Result<>();
        String uname=req.getParameter("uname");
        System.out.println("uname="+uname);
        String upwd=req.getParameter("upwd");
        System.out.println("upwd="+upwd);
        if(StrUtil.isBlank(uname)&&!StrUtil.isBlank(upwd)){
            result.setCode(0);
            result.setMsg("用户名不能为空");
            return result;
        }
        if(!StrUtil.isBlank(uname)&&StrUtil.isBlank(upwd)){
            result.setCode(0);
            result.setMsg("密码不能为空");
            return result;
        }
        if(StrUtil.isBlank(uname)&&StrUtil.isBlank(upwd)){
            result.setCode(0);
            result.setMsg("用户名和密码不能为空");
            return result;
        }
        User user=new User();
        //int tmp=userDao.getLastID();
        //tmp+=1;
        //System.out.println(tmp);
        //Integer userID= tmp;
        //user.setUserId(userID);
        user.setUname(uname);
        user.setUpwd(upwd);
        user.setNick("");
        user.setHead("");
        user.setMotto("");
        int row =userDao.insertUser(user);
        if(row>0){
            result.setCode(1);
            req.getSession().setAttribute("user",user);
            result.setMsg("注册成功！");
        }else{
            result.setCode(0);
            result.setMsg("oops,注册失败");
        }
        return result;
    }

    public Integer checkName(String uname){
        if(StrUtil.isBlank(uname)){

            return 0;
        }
        User user=userDao.queryUserByUname(uname);

        if(user!= null){
            return 0;
        }
        return 1;
    }
}
